/**
 * ListUtil.java
 *
 * Copyright (c) 2009 viorelt All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of viorelt .
 **/
package ro.prjmanager.core.list;

import org.apache.commons.logging.impl.LogFactoryImpl;
import org.apache.commons.logging.Log;

import java.lang.reflect.Field;
import java.util.List;
import java.util.ArrayList;

public class ListUtil {
    private Class clazz;
    private List<Column> columns;
    private String serviceName;
    public static final Log log= LogFactoryImpl.getLog(ListUtil.class);
    public ListUtil(Class clazz) {
        this.clazz = clazz;

    }

    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }


    public void setup(){
        Field[] fields=clazz.getDeclaredFields();
        columns=new ArrayList<Column>();
        if(fields!=null&&fields.length>0)
            for(Field field:fields){
                ro.prjmanager.core.annotations.Column col=field.getAnnotation(ro.prjmanager.core.annotations.Column.class);
                if(col!=null){
                    ro.prjmanager.core.annotations.SelectOption selectOption=
                            field.getAnnotation(ro.prjmanager.core.annotations.SelectOption.class);
                    Column colI=new Column();
                    colI.setName(col.index());
                    if(selectOption!=null){
                        colI.setSelector(true);
                        colI.setIdOption(selectOption.id());
                        colI.setIdOption(selectOption.idOption());
                        colI.setNameOption(selectOption.nameOption());
                        colI.setServiceName(selectOption.serviceName());
                    }
                    columns.add(colI);

                }

            }

        try {
            ro.prjmanager.core.annotations.List lst= clazz.newInstance().getClass().getAnnotation(ro.prjmanager.core.annotations.List.class);
            setServiceName(lst.serviceName());
        } catch (InstantiationException e) {
            log.error(e);
        } catch (IllegalAccessException e) {
            log.error(e);
        }

    }

    public Column findByName(String name){

        if(columns!=null&&!columns.isEmpty())
        for(Column col:columns){
            if(col.getName().equals(name))
                return col;
        }
        return null;
    }
    public Column findBySelId(String idSel){

        if(columns!=null&&!columns.isEmpty())
        for(Column col:columns){
            if(col.getIdSelector().equals(idSel))
                return col;
        }
        return null;
    }

    public String getStringCol(String name){
        String ret;
        Column col=findByName(name);
        if(col!=null&&col.isSelector())
            ret=name+"."+col.getNameOption();
        else ret=name;


        return ret;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
}
